{%- if yandex_pixels -%}
  <!-- Yandex.Metrika counter -->
  <script type="text/javascript">
     (function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
        m[i].l=1*new Date();
        for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }}
        k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
        (window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");

    const yandexPixels = {{yandex_pixels|json}};

    yandexPixels.forEach(function(pixid) {
      ym(pixid, "init", {
        clickmap: true,
        trackLinks: true,
        accurateTrackBounce: true
      });
    });

    function triggerYandexGoal(goal, data) {
      yandexPixels.forEach(function(pixid) {
        ym(pixid, 'reachGoal', goal, data || {});
      });
    }

    window['TOP_GLOBAL_CALLBACK_LAYER'].register({
        'page_view': function() {
          triggerYandexGoal('PageView');
        },
        'view_content': function() {
            triggerYandexGoal('ViewContent');
        },
        'search': function() {
          {% if search.keywords %}
            triggerYandexGoal('Search');
          {% endif %}
        },
        'purchase': function() {
          {% if controller == 'order' and action == 'success' %}
            {% capture yandex_pixels_order_id %}yandex_pixels_{{order.id}}{% endcapture %}
            {% get_cache key={yandex_pixels_order_id} var_name=yandex_pixels_status%}
            {% if yandex_pixels_status ==0 %}
              triggerYandexGoal('Purchase');
            {% endif %}
          {% endif %}
        },
        'begin_checkout': function() {
            triggerYandexGoal('InitiateCheckout');
        },
        'add_to_cart': function(ecdata) {
          triggerYandexGoal('AddToCart');
        },
        'add_to_wishlist': function() {
          triggerYandexGoal('AddToWishlist');
        },
        'sign_up': function() {
          triggerYandexGoal('CompleteRegistration');
        }
    });
  </script>
  <noscript>
    <div><img src="https://mc.yandex.ru/watch/94340395" style="position:absolute; left:-9999px;" alt=""></div>
  </noscript>
{%- endif -%}
